<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/6
 * Time: 10:47
 */
namespace App\Http\Controllers\Api\V2;

use Illuminate\Http\Request;
use Mockery\CountValidator\Exception;
use Validator;
use App\Models\V1\MemberModel;
use App\Models\V1\AudioModel;
use App\Models\V1\BookModel;
use App\Models\V1\ArticleModel;
use DB;


class HomeController extends BaseController
{
    protected $url;
    public function __construct(){
        $this->url = env('ADMIN_HOST');
    }


    /**
     * 首页接口
     *
     */
    public function getHomeList(Request $request)
    {
    
        try {
            $name   = $request->input('mobile', '');
            $key    = $request->input('key', '');
            $oldKey = $request->input('oldKey', '');

            debug_log('api-v2',$key);

            if ($oldKey && $key && $oldKey != $key)
            {
                $db = DB::table('memberinfo')
                    ->where('AppKey', $oldKey)
                    ->where('DeleteMark', 1);
                //检查新的key是否存在 存在表示已经更新
                if ($name) { //两个key都存在 并且不相等 把旧的key改为新的key
                    $db->where('UserName', $name);
                }
                $db->update(['Appkey' => $key]);
            }

            $mstate = 0;
            if ($name) {
                $memberModel = new MemberModel();
                $memberInfo = $memberModel->get_member_info_by_mobile_key($name, $key);
                if ($memberInfo && $memberInfo->isValid) {
                    $mstate = 1; //已付费
                }
            }

            /*每日一言*/
            $audioModel = new AudioModel();
            $audioList = $audioModel->get_audio_all();

            foreach ($audioList as $audio) {

                $params = [
                    'audioId' => $audio->AudioID,
                    'parentId' => 0
                ];
                $params = http_build_query($params);

                $shareInfo = [
                    'title' => "老侯论道",
                    'groupTitle' => $audio->AudioTitle,
                    'description' => $audio->AudioTitle,
                    'img' => env('ADMIN_HOST') . "/images/default-images/share-mp3.png",
                    'link' => env('ADMIN_HOST') . "/home/audio_share_to_weixin?" . $params,
                    "audio" => $audio->FilePath1,

                ];
                $audio->shareInfo = $shareInfo;
            }

            /*热门书籍*/
            $bookModel = new BookModel();
            $hotBooks = $bookModel->get_hot_books();


            /*心灵分享*/
            $articleModel = new ArticleModel();
            $articles = $articleModel->get_article_all();

            if (false) {  //提示升级
                $book = new \stdClass();
                $book->BoHoID = 0;
                $book->LinkUrl = "http://img.laohoulundao.com/video/shuitouzoulewodekehu.mp4";
                $book->FilePath1 = "http://test.wx.laohoulundao.com/images/other/upgrade.png";
                $book->BoHoTitle = "APP升级";
                $book->urlAddress = "https://itunes.apple.com/cn/app/id1187561609?mt=8";
                $book->SortNumber = 0;
                $hotBooks1 = [];
                array_push($hotBooks1, $book);
                foreach ($hotBooks as $b) {
                    array_push($hotBooks1, $b);
                }
                $hotBooks = $hotBooks1;
            }

            $data = [
                'message' => 'ok',
                'code' => '200',
                'mstate' => $mstate,
                'dayslist' => $audioList,
                'hotslist' => $hotBooks,
                'mindsharelist' => $articles,
            ];
                        return response()->json($data);
        }catch (Exception $error){
            return 'ok';
        }
    }



}